package com.das.mechanic_base.mvp.view.create;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.core.content.res.ResourcesCompat;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.adapter.main.ServiceListAdapter;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.bean.even.DeleteFriends;
import com.das.mechanic_base.bean.main.HomeBodyItemBean;
import com.das.mechanic_base.bean.main.ReceiveStatusBean;
import com.das.mechanic_base.mvp.contract.create.X3ServiceListContract;
import com.das.mechanic_base.mvp.presenter.create.X3ServiceListPresenter;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StatusBarUtil;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ViewPagerBind;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = X3AppRouterConstants.Router.SERVICE_LIST)
public class X3ServiceListActivity extends X3BaseActivity<X3ServiceListPresenter> implements X3ServiceListContract.View {

    @BindView(R2.id.tv_title)
    TextView tv_title;
    @BindView(R2.id.fl_header)
    FrameLayout fl_header;
    @BindView(R2.id.tb_layout)
    MagicIndicator tb_layout;
    @BindView(R2.id.vp_main)
    ViewPager vp_main;

    private List<ReceiveStatusBean> mList;

    private ServiceListAdapter itemAdapter;

    @Override
    protected int getLayoutId() {
        EventBus.getDefault().register(this);
        return R.layout.activity_x3_service_list;
    }

    @Override
    protected void initView() {
        X3StatusBarUtil.darkMode(this);
        fl_header.setPadding(0,X3StatusBarUtil.getStatusBarHeight(this),0,0);
        tv_title.setText(getString(R.string.x3_main_pick_work));

        itemAdapter = new ServiceListAdapter(getSupportFragmentManager());
        vp_main.setAdapter(itemAdapter);

        initTabLayout();

        requestReceiveList(0);
    }

    private void initTabLayout() {
        CommonNavigator tbNavigator = new CommonNavigator(this);
        tbNavigator.setScrollPivotX(0.65f);
        tbNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return X3StringUtils.isListEmpty(mList) ? 0 : mList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                String amount = (mList.get(index).receiveBaseAmount == 0) ? "" : " " + mList.get(index).receiveBaseAmount;
                SimplePagerTitleView titleView = new SimplePagerTitleView(context);
                titleView.setText(mList.get(index).statusName + amount);
                titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
                titleView.setTypeface(ResourcesCompat.getFont(context,R.font.pingfan_bold));
                titleView.setNormalColor(Color.parseColor("#b1b3bd"));
                titleView.setSelectedColor(Color.parseColor("#333333"));
                titleView.setOnClickListener(v -> vp_main.setCurrentItem(index));
                return titleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(X3ScreenUtils.dipToPx(3,context));
                indicator.setLineWidth(X3ScreenUtils.dipToPx(16,context));
                indicator.setRoundRadius(X3ScreenUtils.dipToPx(3,context));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(Color.parseColor("#0077ff"));
                return indicator;
            }
        });
        tb_layout.setNavigator(tbNavigator);
        X3ViewPagerBind.bind(tb_layout,vp_main);
    }

    public void requestReceiveList(int showIndex) {
        if (mPresenter != null) {
            mPresenter.requestReceiveStatus(showIndex);
        }
    }

    @Override
    protected X3ServiceListPresenter createPresenter() {
        return new X3ServiceListPresenter();
    }

    @OnClick({R2.id.iv_back,R2.id.iv_add})
    public void onViewClick(View view) {
        if (view.getId() == R.id.iv_back) {
            finish();

        }else if (view.getId() == R.id.iv_add) {
            // 添加接车单
            Intent intent = new Intent(this, X3CreateServiceActivity.class);
            startActivity(intent);
            overridePendingTransition(com.das.mechanic_base.R.anim.activity_enter, 0);
        }
    }

    @Override
    public void getReceiveStatusSuccess(List<ReceiveStatusBean> mList,int showIndex) {
        if (X3StringUtils.isListEmpty(mList)) {
            return;
        }
        this.mList = mList;

        resetBodyFragment(mList,showIndex);
    }

    private void resetBodyFragment(List<ReceiveStatusBean> statusBeans,int showIndex) {
        tb_layout.getNavigator().notifyDataSetChanged();
        itemAdapter.changeFragment(statusBeans);
        itemAdapter.notifyDataSetChanged();

        if (showIndex == -1) {
            int selectIndex = 0;
            for (int i = 0; i < statusBeans.size(); i++) {
                if (statusBeans.get(i).selected) {
                    selectIndex = i;
                    break;
                }
            }
            vp_main.setCurrentItem(selectIndex,false);
        }else {
            vp_main.setCurrentItem(showIndex,false);
        }
    }

    @Override
    public void getReceiveDetailListSuccess(List<HomeBodyItemBean.ListBean> mList) {

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetService(DeleteFriends content) {
        if (content == null) {
            return;
        }
        String type = content.getType();
        if (X3StringUtils.isEmpty(type)) {
            return;
        }
        switch (type) {
            case "Create_Service_Order":
                requestReceiveList(vp_main.getCurrentItem());
                break;
        }
    }
}